-
Notifications
You must be signed in to change notification settings - Fork 230
Move external sampler interface to AbstractMCMC #2704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Turing.jl documentation for PR #2704 is available at: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #2704 +/- ##
============================================
+ Coverage 86.45% 86.52% +0.06%
============================================
Files 21 21
Lines 1418 1410 -8
============================================
- Hits 1226 1220 -6
+ Misses 192 190 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| @@ -1,5 +1,5 @@ | |||
| """ | |||
| ExternalSampler{S<:AbstractSampler,AD<:ADTypes.AbstractADType,Unconstrained} | |||
| ExternalSampler{Unconstrained,S<:AbstractSampler,AD<:ADTypes.AbstractADType} | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this type parameter earlier so that we can dispatch on it more easily.
| 1. Directly implement the `AbstractMCMC.step` methods for `DynamicPPL.Model`. This is the | ||
| most powerful option and is what Turing.jl's in-house samplers do. Implementing this | ||
| means that you can directly call `sample(model, MySampler(), N)`. | ||
| 1. Directly implement the `AbstractMCMC.step` methods for `DynamicPPL.Model`. That is to | ||
| say, implement `AbstractMCMC.step(rng::Random.AbstractRNG, model::DynamicPPL.Model, | ||
| sampler::MySampler; kwargs...)` and related methods. This is the most powerful option and | ||
| is what Turing.jl's in-house samplers do. Implementing this means that you can directly | ||
| call `sample(model, MySampler(), N)`. | ||
| 2. Implement a generic `AbstractMCMC.step` method for `AbstractMCMC.LogDensityModel`. This | ||
| struct wraps an object that obeys the LogDensityProblems.jl interface, so your `step` | ||
| 2. Implement a generic `AbstractMCMC.step` method for `AbstractMCMC.LogDensityModel` (the | ||
| same signature as above except that `model::AbstractMCMC.LogDensityModel`). This struct | ||
| wraps an object that obeys the LogDensityProblems.jl interface, so your `step` | ||
| implementation does not need to know anything about Turing.jl or DynamicPPL.jl. To use | ||
| this with Turing.jl, you will need to wrap your sampler: `sample(model, | ||
| externalsampler(MySampler()), N)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll write proper docs about this separately (in the docs repo).
sunxd3
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no issue
Following on from:
AbstractMCMC.getstatsandAbstractMCMC.requires_unconstrained_spaceAbstractMCMC.jl#182 Adding the interface functionAbstractMCMC.getstatsAbstractMCMC.getstatsAdvancedHMC.jl#471 and implementAbstractMCMC.getstatsAdvancedMH.jl#119 Implementing them for AdvancedHMC and AdvancedMHthis PR changes Turing's external sampler interface to exclusively use AbstractMCMC functions. With this PR, anyone who defines an external sampler will only need to depend on AbstractMCMC (which they presumably already do, because it is a sampler) and LogDensityProblems (which is already a dep of AbstractMCMC). No need for a Turing extension.
To be precise, it makes the following changes:
Turing.Inference.getparams, now one has to defineAbstractMCMC.getparams.AbstractMCMC.getstats.Turing.Inference.isgibbscomponentis changed totrue, so that external sampler packages don't need to override it (unless absolutely necessary).As an example implementation, this PR contains a test mock (note how it doesn't require a Turing dep):
Turing.jl/test/mcmc/external_sampler.jl
Lines 20 to 74 in 06752c4